<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>计算器</title>
	</head>

	<body>
		<p>
			整数1: <input type="text" id="sum1" />
		</p>
		<p>
			整数2: <input type="text" id="sum2" />
		</p>
		<p>
			<button onclick="str(1)">相加</button>
			<button onclick="str(2)">相减</button>
			<button onclick="str(3)">相乘</button>
			<button onclick="str(4)">相除</button>
		</p>
		<p>
			结果： <input type="text" id="sum3" />
		</p>
		<script>
			function str(n) {
				//获取整数1的值
				var sum1 = document.querySelector("#sum1");
				//把值转换成数字
				sum1 = sum1.value;
				//把字符串转换成数字
				sum1 = Number(sum1);
				//获取整数2的值
				var sum2 = document.querySelector("#sum2");
				sum2 = sum2.value;
				//把字符串转换成数字
				sum2 = Number(sum2);
				//获取结果
				var sum3 = document.querySelector("#sum3");

				if(n == 1) {
					sum3.value = sum1 + sum2;
				} else if(n == 2) {
					sum3.value = sum1 - sum2;
				} else if(n == 3) {
					sum3.value = sum1 * sum2;
				} else if(n == 4) {
					sum3.value = sum1 / sum2;
				}

				//console.log(sum1);
				//console.log(sum2);
				//console.log(sum3.value);
				sum3 = sum3.value;
				sum3 = Number(sum3);
				//console.log(sum3);
			}
		</script>
	</body>

</html>